package com.gyc.simple.sysuser.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyc.simple.sysuser.dao.base.IRoleMapper;
import com.gyc.simple.sysuser.dao.base.IRoleSourceMapper;
import com.gyc.simple.sysuser.entity.base.IRole;
import com.gyc.simple.sysuser.entity.base.IRoleSourceKey;
import com.gyc.simple.sysuser.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private IRoleMapper iRoleMapper;
    @Autowired
    private IRoleSourceMapper iRoleSourceMapper;

    @Override
    public PageInfo queryListByPageCondition(Map paramap) {
        PageHelper.startPage(Integer.parseInt((String) paramap.get("pageNum")), Integer.parseInt((String) paramap.get("pageSize")));
        return new PageInfo(iRoleMapper.queryListByPageCondition(paramap));
    }

    @Override
    public boolean addRole(IRole role) {
        iRoleMapper.insertSelective(role);
        List<IRoleSourceKey> relations = new ArrayList<>(role.getList().size());
        for(int i=0;i<role.getList().size();i++){
            IRoleSourceKey iRoleSourceKey = new IRoleSourceKey();
            iRoleSourceKey.setIroleid(role.getIroleid());
            iRoleSourceKey.setIsourceid((String)role.getList().get(i));
            relations.add(iRoleSourceKey);
        }
        return iRoleSourceMapper.batchAddRoleSource(relations)>0;
    }

    @Override
    public List<IRole> selectByUID(String uid) {
        return iRoleMapper.selectByUID(uid);
    }
}
